package com.zlzlib.libui.weight.group;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * @Desc: 自动换行排列得控件
 * @Copyright:
 * @DateTime: 2021/4/19 16:25
 * @Author zlz
 * @Version 1.0
 */
public class WrapView extends ViewGroup {
    private int paddingHor = 0;// 子view水平方向padding
    private int paddingVertical = 0;// 子view垂直方向padding
    private int viewMarginHor = 10;// 子view之间的水平间距
    private int viewMarginVertical = 10;// 行间距

    private int num = 5;// 最多字个数

    /**
     * @param context
     */
    public WrapView(Context context) {
        super(context);
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public WrapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * @param context
     * @param attrs
     */
    public WrapView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int autualWidth = r - l;// 当前容器宽度
        int x = 0;// 横坐标开始
        int y = 0;// 纵坐标开始
        int rows = 1;
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            int width = view.getMeasuredWidth();
            int height = view.getMeasuredHeight();
            x += width + viewMarginHor;
            if (x > autualWidth - viewMarginHor) {// 判断累积高度
                if (i != 0) {
                    x = width + viewMarginHor;
                    rows++;
                }
                // 当一个子view长度超出父view长度时
                if (x > autualWidth - viewMarginHor) {// 判断单个高度
                    // 如果子view是textview的话处理文字
                    if (view instanceof TextView) {
                        TextView tv = ((TextView) view);
                        if (num == 0) {// 只计算一次
                            int wordNum = tv.getText().toString().length();
                            num = wordNum * (autualWidth - 2 * viewMarginHor) / width - 1;
                        }
                        String text = tv.getText().toString();
                        text = text.substring(0, num) + "...";
                        tv.setText(text);
                    }
                    x = autualWidth - viewMarginHor;
                    width = autualWidth - (2 * viewMarginHor);
                }
            }
            y = rows * (height + viewMarginVertical);
            view.layout(x - width, y - height, x, y);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int x = 0;// 横坐标
        int y = 0;// 纵坐标
        int rows = 1;// 总行数
        int specWidth = MeasureSpec.getSize(widthMeasureSpec);
        int actualWidth = specWidth;// 实际宽度
        int childCount = getChildCount();
        for (int index = 0; index < childCount; index++) {
            View child = getChildAt(index);
            child.setPadding(paddingHor, paddingVertical, paddingHor, paddingVertical);
            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int width = child.getMeasuredWidth();
            int height = child.getMeasuredHeight();
            x += width + viewMarginHor;
            if (x > actualWidth - viewMarginHor) {// 换行
                if (index != 0) {
                    x = width + viewMarginHor;
                    rows++;
                }
            }
            y = rows * (height + viewMarginVertical);
        }
        setMeasuredDimension(actualWidth, y + viewMarginVertical);
    }

    public int getPaddingHor() {
        return paddingHor;
    }

    public void setPaddingHor(int paddingHor) {
        this.paddingHor = paddingHor;
    }

    public int getPaddingVertical() {
        return paddingVertical;
    }

    public void setPaddingVertical(int paddingVertical) {
        this.paddingVertical = paddingVertical;
    }

    public int getViewMarginHor() {
        return viewMarginHor;
    }

    public void setViewMarginHor(int viewMarginHor) {
        this.viewMarginHor = viewMarginHor;
    }

    public int getViewMarginVertical() {
        return viewMarginVertical;
    }

    public void setViewMarginVertical(int viewMarginVertical) {
        this.viewMarginVertical = viewMarginVertical;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}